package com.htt.bms.utils;

import com.htt.bms.domain.sys.SysUser;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;


/**
 * @author 侯婷婷
 * @version 1.0
 * @description 用户登录信息
 */
//生成一个无参构造器，且用private修饰
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SecurityUserUtils {

    //获取身份认证
    //Authentication: 身份认证
    //Authorization: 授权
    public static Authentication getAuthentication(){
        //SecurityContextHolder: 是spring security存储当前上下文详细信息的地方
        //                       .当前已认证用户
        //                       .用户的授予权限
        //                       .其他相关安全细节
        return SecurityContextHolder.getContext().getAuthentication();
    }


    public static SysUser getUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (SysUser) authentication.getDetails();
    }

    public static void logout(){
        SecurityContextHolder.clearContext();
    }
}
